package com.guice.web.guice_webapp_example.servlet;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.guice.web.guice_webapp_example.mybatis.domain.User;
import com.guice.web.guice_webapp_example.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Singleton
public class UserServlet extends HttpServlet {
    @Inject
    UserService userService;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        Integer age = Integer.valueOf(req.getParameter("age"));
        String address = req.getParameter("address");
        User user = userService.save(name,age,address);
        resp.getOutputStream().write(user.toString().getBytes());
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getOutputStream().print(userService.findById(2).toString());
    }
}
